package com.lottery.im.module.main.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.lottery.im.R;
import com.lottery.im.module.main.adapter.PreviewImageAdapter;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.widget.indicator.CirclePageIndicator;
import com.lottery.im.widget.photoview.HackyViewPager;
import com.lottery.im.widget.photoview.PhotoViewAttacher;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片预览界面
 */
public class ImagePreviewActivity extends AppCompatActivity {

  private static String PICTYPE = "PICTYPE";
  private static String POSITION = "POSITION";
  private static String PICPATHS = "PICPATHS";

  public static void create(Context context, int picType, int position,
      ArrayList<String> picPaths) {
    Intent intent = new Intent(context, ImagePreviewActivity.class);
    intent.putExtra(PICTYPE, picType);
    intent.putExtra(POSITION, position);
    intent.putStringArrayListExtra(PICPATHS, picPaths);
    context.startActivity(intent);
  }

  private static final String ISLOCKED_ARG = "isLocked";

  private static List<String> mPicPaths;
  private static int mPicType;
  private static int mPosition;

  public static int URL_TYPE = 1;
  public static int PATH_TYPE = 2;

  @BindView(R.id.view_pager) HackyViewPager mViewPager;
  @BindView(R.id.indicator) CirclePageIndicator mIndicator;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    StatusBarHelper.translucent(this, 0xFF000000);

    setContentView(R.layout.activity_image_preview);

    ButterKnife.bind(this);

    mPicType = getIntent().getIntExtra(PICTYPE, PATH_TYPE);
    mPosition = getIntent().getIntExtra(POSITION, 2);
    mPicPaths = getIntent().getStringArrayListExtra(PICPATHS);

    PreviewImageAdapter samplePagerAdapter = new PreviewImageAdapter(mPicPaths, mPicType);

    samplePagerAdapter.setPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
      @Override public void onPhotoTap(View view, float x, float y) {
        //单击图片
        finish();
      }

      @Override public void onOutsidePhotoTap() {
        finish();
      }
    });
    mViewPager.setAdapter(samplePagerAdapter);
    mIndicator.setViewPager(mViewPager);
    mViewPager.setCurrentItem(mPosition);

    if (savedInstanceState != null) {
      boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false);
      mViewPager.setLocked(isLocked);
    }
  }

  private boolean isViewPagerActive() {
    return (mViewPager != null && mViewPager instanceof HackyViewPager);
  }

  @Override public void onSaveInstanceState(Bundle outState) {
    if (isViewPagerActive()) {
      outState.putBoolean(ISLOCKED_ARG, mViewPager.isLocked());
    }
    super.onSaveInstanceState(outState);
  }
}
